#include "syssqlinterpreter.h"
#include <QFile>
#include <QDateTime>
#include <QDebug>
#include <QRegularExpression>
#include <QMessageBox>

SysSqlInterpreter::SysSqlInterpreter()
{

}

bool SysSqlInterpreter::f_replaceSqlString(CnsComponentPtr com, const QSSHASH &patterns, QString &sqlString)
{
    for(QSSHASH::const_iterator it = patterns.begin(); it != patterns.end(); it ++)
    {
        sqlString.replace(it.key(), it.value());
    }
    return true;
}

bool SysSqlInterpreter::f_start(uint8_t &breakoff, CnsComponentPtr com)
{
    QString sqlString = com->property("sql").toString();
    if(getSqlStringFromFile(breakoff, sqlString))
    {
        const QString &cacheName = com->property("cache").toString();
        com->setProperty(sqlString, cacheName);
        return true;
    }
    return false;
}

bool SysSqlInterpreter::getSqlStringFromFile(uint8_t &breakoff, QString &sqlString)
{
    //origin sqlString is file path
    QFile file( sqlString );
    if(file.exists()) {
        if(file.open( QIODevice::ReadOnly ))
        {
            sqlString = file.readAll();
            return true;
        } else{
            breakoff = true;
            qCritical() << "file '" << sqlString << "' can't open";
            return false;
        }
    }
    else {
        breakoff = true;
        qCritical() << "file '" << sqlString << "' not exists";
        return false;
    }
}
